package com.teng.mylibrary.ui.activity

import android.app.AlertDialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.DialogInterface
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import com.teng.mylibrary.R
import com.teng.mylibrary.base.BaseActivity
import com.teng.mylibrary.base.BaseViewModel
import com.teng.mylibrary.databinding.ActivityCrashBinding

class CrashActivity : BaseActivity<ActivityCrashBinding, BaseViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_crash
    }

    override fun initViewModel(): BaseViewModel {
        return ViewModelProvider(this).get(BaseViewModel::class.java)
    }

    override fun initView() {
        title = "问题反馈"
        supportActionBar?.setDisplayHomeAsUpEnabled(false)

    }

    override fun initData() {
        val config = CustomActivityOnCrash.getConfigFromIntent(intent)

        if (config == null) {
            //This should never happen - Just finish the activity to avoid a recursive crash.
            finish()
            return
        }

        if (config.isShowRestartButton && config.restartActivityClass != null) {
            binding.btnRestart.text = "重启应用"
            binding.btnRestart.setOnClickListener(View.OnClickListener {
                CustomActivityOnCrash.restartApplication(
                    this@CrashActivity,
                    config
                )
            })
        } else {
            binding.btnRestart.setOnClickListener(View.OnClickListener {
                CustomActivityOnCrash.closeApplication(
                    this@CrashActivity,
                    config
                )
            })
        }

        if (config.isShowErrorDetails) {
            binding.tvDetail.setOnClickListener(View.OnClickListener { //We retrieve all the error data and show it
                val dialog: AlertDialog = AlertDialog.Builder(this@CrashActivity)
                    .setTitle("错误详情")
                    .setMessage(
                        CustomActivityOnCrash.getAllErrorDetailsFromIntent(
                            this@CrashActivity,
                            intent
                        )
                    )
                    .setPositiveButton(
                        "关闭",
                        null
                    )
                    .setNeutralButton("复制",
                        DialogInterface.OnClickListener { dialog, which -> copyErrorToClipboard() })
                    .show()
                val textView: TextView = dialog.findViewById(android.R.id.message)
                textView.setTextSize(
                    TypedValue.COMPLEX_UNIT_PX,
                    resources.getDimension(R.dimen.customactivityoncrash_error_activity_error_details_text_size)
                )
            })
        } else {
            binding.tvDetail.setVisibility(View.GONE)
        }
    }

    private fun copyErrorToClipboard() {
        val errorInformation = CustomActivityOnCrash.getAllErrorDetailsFromIntent(
            this@CrashActivity,
            intent
        )
        val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager

        //Are there any devices without clipboard...?
        val clip = ClipData.newPlainText(
            "错误详情",
            errorInformation
        )
        clipboard.setPrimaryClip(clip)
        showToastOkMsg("复制成功")
    }
}